package com.x.erp.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.x.erp.model.MultilevelActivityShare;
import com.x.erp.rpc.MultilevelActivityShareRpcService;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 多级营销活动分享记录信息
 */
@RestController
@RequestMapping("/multilevelActivityShare")
public class MultilevelActivityShareController extends BaseController{

	@Reference
	private MultilevelActivityShareRpcService multilevelActivityShareRpcService;// 多级营销活动分享记录信息服务

	/**
	 * 查询多级营销活动分享记录信息列表
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getMultilevelActivityShareList", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getMultilevelActivityShareList(MultilevelActivityShare multilevelActivityShare) {
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", multilevelActivityShareRpcService.selectMultilevelActivityShareList(multilevelActivityShare));
	}

	/**
	 * 查询多级营销活动分享记录信息数量
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getMultilevelActivityShareNum", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getMultilevelActivityShareNum(MultilevelActivityShare multilevelActivityShare) {
		Map<String, Object> res = new HashMap<>();
		res.put("total", multilevelActivityShareRpcService.selectMultilevelActivityShareNum(multilevelActivityShare));
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 根据ID查询多级营销活动分享记录信息
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getMultilevelActivityShare", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getMultilevelActivityShare(MultilevelActivityShare multilevelActivityShare) {
		if(multilevelActivityShare == null || StringUtils.isEmpty(multilevelActivityShare.getShareId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "分享ID不能为空！");
		}
		//查询app当前版本
		List<MultilevelActivityShare> multilevelActivityShares = multilevelActivityShareRpcService.selectMultilevelActivityShareList(multilevelActivityShare);
		if(multilevelActivityShares != null && multilevelActivityShares.size() > 0) {
			return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", multilevelActivityShares.get(0));
		}
		return new ResultModel(ResultCode.RESULT_FAIL, "查询失败");
	}

	/**
	 * 新增多级营销活动分享记录信息
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/insertMultilevelActivityShare", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel insertMultilevelActivityShare(MultilevelActivityShare multilevelActivityShare) {
		if(multilevelActivityShare == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "多级营销活动分享记录信息不能为空！");
		}
		if(StringUtils.isEmpty(multilevelActivityShare.getActivityId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "活动ID不能为空！");
		}
		if(StringUtils.isEmpty(multilevelActivityShare.getUserId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "用户ID不能为空！");
		}
		MultilevelActivityShare share = new MultilevelActivityShare();
		share.setUserId(multilevelActivityShare.getUserId());
		share.setActivityId(multilevelActivityShare.getActivityId());
		Date cDate = this.getDate(0);
		multilevelActivityShare.setShareState(0);
		multilevelActivityShare.setShareCreateTime(cDate);
		multilevelActivityShare.setShareUpdateTime(cDate);
		multilevelActivityShare = multilevelActivityShareRpcService.insertMultilevelActivityShare(multilevelActivityShare);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "插入成功！", multilevelActivityShare);
	}

	/**
	 * 分享成功
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/shareSuccess", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel shareSuccess(String shareId){
		if(StringUtils.isEmpty(shareId)){
			return new ResultModel(ResultCode.RESULT_FAIL, "分享ID不能为空！");
		}
		MultilevelActivityShare share = new MultilevelActivityShare();
		share.setShareId(shareId);
		share.setShareState(1);
		multilevelActivityShareRpcService.updateMultilevelActivityShare(share);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "分享成功！");
	}

	/**
	 * 分享失败
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/shareFail", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel shareFail(String shareId){
		if(StringUtils.isEmpty(shareId)){
			return new ResultModel(ResultCode.RESULT_FAIL, "分享ID不能为空！");
		}
		MultilevelActivityShare share = new MultilevelActivityShare();
		share.setShareId(shareId);
		share.setShareState(0);
		multilevelActivityShareRpcService.deleteMultilevelActivityShare(share);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "分享成功！");
	}

}
